//
//  HomeRequestModel.swift
//  TianTianBiTing
//
//  Created by Gox mac 2 on 2018/12/21.
//  Copyright © 2018 Gox_WallyDiao. All rights reserved.
//

import UIKit
import Alamofire
import SwiftyJSON
import HandyJSON

class HomeRequestModel {
    class func downMP3(url:String, completion:@escaping (_ success:Bool) -> ()) {
        TTNetworkTools.downLoadMP3(downloadfileURL: url) { (json) in
//            print(json)
        }
    }
    
    class func getIOSAudit(completion:@escaping (_ success:Bool, _ userId:Int) -> ()) {
        TTNetworkTools.getRequestData(URLString: TTURL_IOSAudit, jointArr: nil, parameters: nil) { (json) in
            if json["code"].stringValue == SUCCESS {
                let dic = json["data"].dictionaryValue
                completion(true,dic["id"]?.intValue ?? 46158)
            } else {
                completion(false,0)
            }
            
        }
    }
    class func loadHomeTopBannerData(completion:@escaping (_ succeed: Bool,_ message: String,_ bannerList: [TopBannerImagesModel]?) -> ()) {
        TTNetworkTools.getRequestData(URLString: TTURL_Index, jointArr: nil, parameters: ["type":1]) { (json) in
            if json["code"].stringValue == SUCCESS {
                var bannerModels = [TopBannerImagesModel]()
                let arr = json["data"].arrayValue
                for dic in arr {
                    let model = JSONDeserializer<TopBannerImagesModel>.deserializeFrom(dict: dic.dictionaryObject)
                    bannerModels.append(model!)
                }
                completion(true, json["message"].stringValue, bannerModels)
            } else {
                completion(false, json["message"].stringValue, nil)
            }
        }
    }
    
    class func loadHomeItemsData(completion:@escaping (_ succeed: Bool,_ message: String,_ list: [CentreItemImagesModel]?) -> ()) {
        TTNetworkTools.getRequestData(URLString: TTURL_Index, jointArr: nil, parameters: ["type":2]) { (json) in
            if json["code"].stringValue == SUCCESS {
                var models = [CentreItemImagesModel]()
                let arr = json["data"].arrayValue
                for dic in arr {
                    let model = JSONDeserializer<CentreItemImagesModel>.deserializeFrom(dict: dic.dictionaryObject)
                    models.append(model!)
                }
                completion(true, json["message"].stringValue, models)
            } else {
                completion(false, json["message"].stringValue, nil)
            }
        }
    }
    
    class func loadOtherListenData(completion:@escaping (_ succeed: Bool,_ message: String,_ bannerList: [OtherListenModel]?) -> ()) {
        TTNetworkTools.getRequestData(URLString: TTURL_AllCategoryProgram, jointArr: nil, parameters: ["userId":String.getUserId()]) { (json) in
            if json["code"].stringValue == SUCCESS {
                var bannerModels = [OtherListenModel]()
                let arr = json["data"].arrayValue
                for dic in arr {
                    let model = JSONDeserializer<OtherListenModel>.deserializeFrom(dict: dic.dictionaryObject)
                    bannerModels.append(model!)
                }
                completion(true, json["message"].stringValue, bannerModels)
            } else {
                completion(false, json["message"].stringValue, nil)
            }
        }
    }
    
    class func loadListenListData(categoryId:Int, parameters:[String:Any], completion:@escaping (_ succeed: Bool,_ message: String,_ bannerList: [ListenListModel]?) -> ()) {
        TTNetworkTools.getRequestData(URLString: TTURL_Program, jointArr: [categoryId], parameters: parameters) { (json) in
            if json["code"].stringValue == SUCCESS {
                var bannerModels = [ListenListModel]()
                let dic = json["data"].dictionaryValue
                let arr = dic["data"]!.arrayValue
                for dic in arr {
                    let model = JSONDeserializer<ListenListModel>.deserializeFrom(dict: dic.dictionaryObject)
                    bannerModels.append(model!)
                }
                completion(true, json["message"].stringValue, bannerModels)
            } else {
                completion(false, json["message"].stringValue, nil)
            }
        }
    }
    
    class func loadPlayListData(programId:Int,parameters:[String:Any], completion:@escaping (_ succeed: Bool,_ message: String,_ bannerList: [PlayListModel]?, _ totalpage:Int?) -> ()) {
        TTNetworkTools.getRequestData(URLString: TTURL_Seasons, jointArr: [programId], parameters: parameters) { (json) in
            if json["code"].stringValue == SUCCESS {
                var bannerModels = [PlayListModel]()
                let dic = json["data"].dictionaryValue
                let arr = dic["data"]!.arrayValue
                for dic in arr {
                    let model = JSONDeserializer<PlayListModel>.deserializeFrom(dict: dic.dictionaryObject)
                    bannerModels.append(model!)
                }
                completion(true, json["message"].stringValue, bannerModels,dic["totalPages"]?.intValue)
            } else {
                completion(false, json["message"].stringValue, nil,0)
            }
        }
    }
    
    class func loadPlayDetailsData(programId:[Int], completion:@escaping (_ succeed: Bool,_ message: String,_ bannerList: PlayDetailsModel?) -> ()) {
        TTNetworkTools.getRequestData(URLString: TTURL_ProgramDetail, jointArr: programId, parameters: nil) { (json) in
            if json["code"].stringValue == SUCCESS {
                var bannerModels = PlayDetailsModel()
                bannerModels = JSONDeserializer<PlayDetailsModel>.deserializeFrom(dict: json["data"].dictionaryObject)!
                completion(true, json["message"].stringValue, bannerModels)
            } else {
                completion(false, json["message"].stringValue, nil)
            }
        }
    }
    
    class func delHistory(parameters:[String : Any], completion:@escaping (_ succeed: Bool) -> ()) {
        TTNetworkTools.postRequestData(URLString: TTURL_DelHistoryMark, jointArr: nil, parameters: parameters) { (json) in
            if json["code"].stringValue == SUCCESS {
                completion(true)
            } else {
                completion(false)
            }
        }
    }
    
    class func increaseHistoryAndCollections(parameters:[String : Any], completion:@escaping (_ succeed: Bool) -> ()) {
        TTNetworkTools.postRequestData(URLString: TTURL_Mark, jointArr: nil, parameters: parameters) { (json) in
            if json["code"].stringValue == SUCCESS {
                completion(true)
            } else {
                completion(false)
            }
        }
    }
    
    class func recordListeningHistory(parameters:[String : Any], completion:@escaping (_ succeed: Bool) -> ()) {
        TTNetworkTools.postRequestData(URLString: TTURL_PopularRecord, jointArr: nil, parameters: parameters) { (json) in
            if json["code"].stringValue == SUCCESS {
                completion(true)
            } else {
                completion(false)
            }
        }
    }
    
    class func loadHistoryAndCollectData(programId:[Int],parameters:[String : Any], completion:@escaping (_ succeed: Bool,_ message: String,_ bannerList: [ListenListModel]?, _ totalpage:Int?) -> ()) {
        TTNetworkTools.getRequestData(URLString: TTURL_Status4Mark, jointArr: programId, parameters: parameters) { (json) in
            if json["code"].stringValue == SUCCESS {
                var bannerModels = [ListenListModel]()
                let dic = json["data"].dictionaryValue
                let arr = dic["data"]!.arrayValue
                for dic in arr {
                    let model = JSONDeserializer<ListenListModel>.deserializeFrom(dict: dic.dictionaryObject)
                    bannerModels.append(model!)
                }
                completion(true, json["message"].stringValue, bannerModels, dic["totalPages"]?.intValue)
            } else {
                completion(false, json["message"].stringValue, nil,0)
            }
        }
    }
    
    class func loadProgram(categoryId:[Int],parameters:[String : Any], completion:@escaping (_ succeed: Bool,_ message: String,_ bannerList: [ListenListModel]?, _ totalpage:Int?) -> ()) {
        TTNetworkTools.getRequestData(URLString: TTURL_Program, jointArr: categoryId, parameters: parameters) { (json) in
            if json["code"].stringValue == SUCCESS {
                var bannerModels = [ListenListModel]()
                let dic = json["data"].dictionaryValue
                let arr = dic["data"]!.arrayValue
                for dic in arr {
                    let model = JSONDeserializer<ListenListModel>.deserializeFrom(dict: dic.dictionaryObject)
                    bannerModels.append(model!)
                }
                completion(true, json["message"].stringValue, bannerModels, dic["totalPages"]?.intValue)
            } else {
                completion(false, json["message"].stringValue, nil,0)
            }
        }
    }
    
    class func loadSearch(parameters:[String : Any], completion:@escaping (_ succeed: Bool,_ message: String,_ bannerList: [ListenListModel]?) -> ()) {
        TTNetworkTools.getRequestData(URLString: TTURL_Search, jointArr: nil, parameters: parameters) { (json) in
            if json["code"].stringValue == SUCCESS {
                var bannerModels = [ListenListModel]()
                let arr = json["data"].arrayValue
                for dic in arr {
                    let model = JSONDeserializer<ListenListModel>.deserializeFrom(dict: dic.dictionaryObject)
                    bannerModels.append(model!)
                }
                completion(true, "", bannerModels)
            } else {
                completion(false, json["message"].stringValue, nil)
            }
        }
    }
    
    class func loadCateBannerList(parameters:[String : Any], completion:@escaping (_ succeed: Bool,_ message: String,_ bannerList: [CommonBannerListModel]?) -> ()) {
        TTNetworkTools.getRequestData(URLString: TTURL_CommonBannerList, jointArr: nil, parameters: parameters) { (json) in
            if json["code"].stringValue == SUCCESS {
                var bannerModels = [CommonBannerListModel]()
                let arr = json["data"].arrayValue
                for dic in arr {
                    let model = JSONDeserializer<CommonBannerListModel>.deserializeFrom(dict: dic.dictionaryObject)
                    bannerModels.append(model!)
                }
                completion(true, json["message"].stringValue, bannerModels)
            } else {
                completion(false, json["message"].stringValue, nil)
            }
        }
    }
    
    class func loadBatchSeasonsData(parameters:[String : Any], completion:@escaping (_ succeed: Bool,_ message: String,_ list: PartSeasonModel?) -> ()) {
        TTNetworkTools.getRequestData(URLString: TTURL_BatchSeasons, jointArr: nil, parameters: parameters) { (json) in
            if json["code"].stringValue == SUCCESS {
                let dic = json["data"].dictionaryObject
                let model = JSONDeserializer<PartSeasonModel>.deserializeFrom(dict: dic)
                completion(true, json["message"].stringValue, model)
            } else {
                completion(false, json["message"].stringValue, nil)
            }
        }
    }
    
    class func buyPartSeason(parameters:[String : Any], completion:@escaping (_ succeed: Bool,_ message: String,_ state:String) -> ()) {
        TTNetworkTools.getRequestData(URLString: TTURL_WalletRecordEvery, jointArr: nil, parameters: parameters) { (json) in
            if json["code"].stringValue == SUCCESS {
                let dic = json["data"].dictionaryValue
                let disp = dic["disPrice"]?.stringValue
                let bo:Bool = disp?.hasPrefix("-") ?? false
                if bo {
                    completion(false,"余额不足，请前去充值","2")
                } else {
                    completion(true,"购买成功，请继续购买","1")
                }
            } else {
                completion(false,json["message"].stringValue,"3")
            }
        }
    }
    
    class func buy10Season(parameters:[String : Any], completion:@escaping (_ succeed: Bool,_ message: String,_ state:String) -> ()) {
        TTNetworkTools.getRequestData(URLString: TTURL_Buy10Season, jointArr: nil, parameters: parameters) { (json) in
            if json["code"].stringValue == SUCCESS {
                let dic = json["data"].dictionaryValue
                let disp = dic["disPrice"]?.stringValue
                let bo:Bool = disp?.hasPrefix("-") ?? false
                if bo {
                    completion(false,"余额不足，请前去充值","2")
                } else {
                    completion(true,"购买成功，请继续购买","1")
                }
            } else {
                completion(false,json["message"].stringValue,"3")
            }
        }
    }
    
    class func get10SeasonPrice(parameters:[String : Any], completion:@escaping (_ price:NSNumber) -> ()) {
        TTNetworkTools.getRequestData(URLString: TTURL_Count10Season, jointArr: nil, parameters: parameters) { (json) in
            if json["code"].stringValue == SUCCESS {
                let dic = json["data"].dictionaryValue
                
                completion(dic["totalPrice"]?.numberValue ?? 0)
            } else {
                completion(0)
            }
        }
    }
    
    class func get10SeasonCount(parameters:[String : Any], completion:@escaping (_ count:Int) -> ()) {
        TTNetworkTools.getRequestData(URLString: TTURL_totalSeason, jointArr: nil, parameters: parameters) { (json) in
            if json["code"].stringValue == SUCCESS {
                let dic = json["data"].dictionaryValue
                
                completion(dic["totalSeason"]?.intValue ?? 0)
            } else {
                completion(0)
            }
        }
    }

    class func buyAllSeason(parameters:[String : Any], completion:@escaping (_ succeed: Bool,_ message: String,_ state:String) -> ()) {
        TTNetworkTools.postRequestData(URLString: TTURL_WalletRecord, jointArr: nil, parameters: parameters) { (json) in
            if json["code"].stringValue == SUCCESS {
                completion(true,json["message"].stringValue,"1")
            } else if json["code"].stringValue == "025" {
                
                completion(false,json["message"].stringValue,"2")
            } else {
                
                completion(false,json["message"].stringValue,"3")
            }
        }
    }
    
    class func putIDFA(parameters:[String : Any]) {
        TTNetworkTools.getRequestData(URLString: TTURL_IDFA, jointArr: nil, parameters: parameters) { (json) in
            
        }
    }
    
    class func getCollecState(parameters:[String : Any], completion:@escaping (_ state:Bool) -> ()) {
        TTNetworkTools.getRequestData(URLString: TTURL_isMark, jointArr: nil, parameters: parameters) { (json) in
            if json["code"].stringValue == SUCCESS {
                let dic = json["data"].dictionaryValue
                if dic["isMark"]?.boolValue == true {
                    completion(true)
                } else {
                    completion(false)
                }
            } else {
                completion(false)
            }
            
        }
    }
    
    class func uploadingNowDate(parameters:[String : Any], completion:@escaping (_ succeed: Bool) -> ()) {
        TTNetworkTools.getRequestData(URLString: TTURL_OperationRecord, jointArr: nil, parameters: parameters) { (json) in
            if json["code"].stringValue == SUCCESS {
                completion(true)
            } else {
                completion(false)
            }
        }
    }
    
    class func uploadingListenDate(parameters:[String : Any]) {
        TTNetworkTools.postRequestData(URLString: TTURL_Honbao, jointArr: nil, parameters: parameters) { (json) in
           
        }
    }
    
    class func loadHotSeatchWordData(completion:@escaping (_ succeed: Bool,_ message: String,_ list: [HotSearchWordModel]?) -> ()) {
        TTNetworkTools.getRequestData(URLString: TTURL_HotSearchWord, jointArr: nil, parameters: nil) { (json) in
            if json["code"].stringValue == SUCCESS {
                var bannerModels = [HotSearchWordModel]()
                let arr = json["data"].arrayValue
                for dic in arr {
                    let model = JSONDeserializer<HotSearchWordModel>.deserializeFrom(dict: dic.dictionaryObject)
                    bannerModels.append(model!)
                }
                completion(true, json["message"].stringValue, bannerModels)
            } else {
                completion(false, json["message"].stringValue, nil)
            }
        }
    }
    
    class func loadDefaultSeatchWordData(completion:@escaping (_ succeed: Bool,_ message: String,_ list: [HotSearchWordModel]?) -> ()) {
        TTNetworkTools.getRequestData(URLString: TTURL_HotSearchWord, jointArr: nil, parameters: nil) { (json) in
            if json["code"].stringValue == SUCCESS {
                var bannerModels = [HotSearchWordModel]()
                let arr = json["data"].arrayValue
                for dic in arr {
                    let model = JSONDeserializer<HotSearchWordModel>.deserializeFrom(dict: dic.dictionaryObject)
                    bannerModels.append(model!)
                }
                completion(true, json["message"].stringValue, bannerModels)
            } else {
                completion(false, json["message"].stringValue, nil)
            }
        }
    }
    
    class func loadRandomData(parameters:[String : Any], completion:@escaping (_ succeed: Bool,_ message: String,_ state:Int,_ price:NSNumber) -> ()) {
        TTNetworkTools.postRequestData(URLString: TTURL_Random, jointArr: nil, parameters: parameters) { (json) in
            if json["code"].stringValue == SUCCESS {
                let dic = json["data"].dictionaryValue
                completion(true,json["message"].stringValue,dic["state"]?.intValue ?? 0,dic["tb"]?.numberValue ?? 0.0)
            } else {
                completion(false,json["message"].stringValue, 0, 0.0)
            }
        }
    }
    
    class func loadSavetbData(parameters:[String : Any], completion:@escaping (_ succeed: Bool,_ message: String) -> ()) {
        TTNetworkTools.postRequestData(URLString: TTURL_Savetb, jointArr: nil, parameters: parameters) { (json) in
            if json["code"].stringValue == SUCCESS {
                completion(true,"")
            } else {
                completion(false,json["message"].stringValue)
            }
        }
    }
    
    class func UpdateJPushId(parameters:[String : Any], completion:@escaping (_ succeed: Bool) -> ()) {
        TTNetworkTools.postRequestData(URLString: TTURL_UpdateJPushId, jointArr: nil, parameters: parameters) { (json) in
            if json["code"].stringValue == SUCCESS {
                completion(true)
            } else {
                completion(false)
            }
        }
    }
    
    class func loadDiscussData(parameters:[String : Any], completion:@escaping (_ succeed: Bool,_ message: String,_ list: [CommentModel]?) -> ()) {
        TTNetworkTools.getRequestData(URLString: TTURL_Discuss, jointArr: nil, parameters: parameters) { (json) in
            if json["code"].stringValue == SUCCESS {
                var bannerModels = [CommentModel]()
                let dic = json["data"].dictionaryValue
                let arr = dic["data"]!.arrayValue
                for dic in arr {
                    let model = JSONDeserializer<CommentModel>.deserializeFrom(dict: dic.dictionaryObject)
                    bannerModels.append(model!)
                }
                completion(true, json["message"].stringValue, bannerModels)
            } else {
                completion(false, json["message"].stringValue, nil)
            }
        }
    }
    
    class func loadDiscussLevel1Data(parameters:[String : Any], completion:@escaping (_ succeed: Bool,_ message: String,_ list: [CommentModel]?) -> ()) {
        TTNetworkTools.getRequestData(URLString: TTURL_DiscussDiscussLevel1, jointArr: nil, parameters: parameters) { (json) in
            if json["code"].stringValue == SUCCESS {
                var bannerModels = [CommentModel]()
                let arr = json["data"].arrayValue
                for dic in arr {
                    let model = JSONDeserializer<CommentModel>.deserializeFrom(dict: dic.dictionaryObject)
                    bannerModels.append(model!)
                }
                completion(true, json["message"].stringValue, bannerModels)
            } else {
                completion(false, json["message"].stringValue, nil)
            }
        }
    }
    
    class func UpdateDiscussProgram(parameters:[String : Any], completion:@escaping (_ succeed: Bool, _ message:String) -> ()) {
        TTNetworkTools.postRequestData(URLString: TTURL_DiscussProgram, jointArr: nil, parameters: parameters) { (json) in
            if json["code"].stringValue == SUCCESS {
                completion(true, "")
            } else {
                completion(false, json["message"].stringValue)
            }
        }
    }
    
    class func loadResourcesData(parameters:[String : Any], completion:@escaping (_ succeed: Bool,_ message: String,_ url:String) -> ()) {
        TTNetworkTools.getRequestData(URLString: TTURL_Resources, jointArr: nil, parameters: parameters) { (json) in
            if json["code"].stringValue == SUCCESS {
                let dic = json["data"].dictionaryValue
                completion(true,"",dic["location"]?.stringValue ?? "")
            } else {
                completion(false,json["message"].stringValue, "")
            }
        }
    }
    
    class func UpdateMyProgramSharePost(parameters:[String : Any], completion:@escaping (_ succeed: Bool, _ message:String) -> ()) {
        TTNetworkTools.postRequestData(URLString: TTURL_MyProgramSharePost, jointArr: nil, parameters: parameters) { (json) in
            if json["code"].stringValue == SUCCESS {
                completion(true, "")
            } else {
                completion(false, json["message"].stringValue)
            }
        }
    }
    
    class func loadWXPicData(parameters:[String : Any],completion:@escaping (_ succeed: Bool,_ message: String,_ pic: String) -> ()) {
        TTNetworkTools.getRequestData(URLString: TTURL_PayWaiterWXPic, jointArr: nil, parameters: parameters) { (json) in
            if json["code"].stringValue == SUCCESS {
                let dic = json["data"].dictionaryValue
                completion(true,"",dic["location"]?.stringValue ?? "")
            } else {
                completion(false,json["message"].stringValue, "")
            }
        }
    }
    
    
    
}


